package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Namespace("/Directories/TeachingCommittee")
public class TeachingCommitteeController extends ActionSupport implements ModelDriven {
    private TeachingCommitteeCRUDService service;
    private RoleCRUDService roleService;
    private ContactPersonService contactPersonService;
    private AddressService addressService;
    private CenterCRUDService centroService;

    private List<ComiteDocencia> list = new ArrayList<ComiteDocencia>();
    private ComiteDocencia comiteDocencia = new ComiteDocencia();
    private String editableCommitteeId;
    private String editContactPersonId;
    private List<Countries> countries = new ArrayList<Countries>();
    private List<Regions> regions = new ArrayList<Regions>();
    private List<Cities> cities = new ArrayList<Cities>();
    private Countries countrySelected;
    private Regions regionSelected;
    private Cities citySelected;
    private ContactPerson contactPerson = new ContactPerson();
    private List<Role> roles;
    private Collection<ContactPerson> contactList;
    private List<Centro> centers = new ArrayList<Centro>();


    @Autowired
    public TeachingCommitteeController(TeachingCommitteeCRUDService service,
                                       AddressService addressService,
                                       RoleCRUDService roleService,
                                       ContactPersonService contactPersonService,
                                       CenterCRUDService centroService) {
        this.service = service;
        this.roleService = roleService;
        this.addressService = addressService;
        comiteDocencia.setAddress(new Address());
        countries = this.addressService.getCountries();
        this.contactPersonService = contactPersonService;
        this.centroService = centroService;
        roles = this.roleService.findSiteRole();
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.teachingCommittee.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<ComiteDocencia>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.teachingCommittee.tiles", type="tiles")
    })
    public String list() {

        CriteriaTeachingCommittee cr = new CriteriaTeachingCommittee(comiteDocencia);
        list = service.list(cr);

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.teachingCommittee.create.tiles", type="tiles")
    })
    public String create() {
        if(editableCommitteeId != null){
            int id = Integer.parseInt(editableCommitteeId);
            comiteDocencia = service.findById(id);

            countrySelected = addressService.getCountryByName(comiteDocencia.getAddress().getCountry());

            regions = addressService.getRegions(countrySelected.getCountryId());
            regionSelected = addressService.getRegionByName(comiteDocencia.getAddress().getProvince(), countrySelected.getCountryId().toString());

            cities = addressService.getCities(regionSelected.getRegionId());

            citySelected = addressService.getCityByName(comiteDocencia.getAddress().getCity(), regionSelected.getRegionId().toString(), countrySelected.getCountryId().toString());

        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {
        addressService.checkCity(   comiteDocencia.getAddress().getCity(),
                comiteDocencia.getAddress().getProvince(),
                comiteDocencia.getAddress().getCountry()
        );
        if(comiteDocencia.getCentro()!=null){
            Centro centro = centroService.findById(comiteDocencia.getCentro().getId());
            comiteDocencia.setCentro(centro);
        }
        service.save(comiteDocencia);
        return SUCCESS;
    }


    @Override
    public Object getModel() {
        return comiteDocencia;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String delete() {
        if(editableCommitteeId != null){
            int id = Integer.parseInt(editableCommitteeId);
            comiteDocencia = service.findById(id);
            service.delete(comiteDocencia);
        }
        return SUCCESS;
    }

    public List<ComiteDocencia> getList() {
        return list;
    }

    public void setList(List<ComiteDocencia> list) {
        this.list = list;
    }

    public ComiteDocencia getComiteDocencia() {
        return comiteDocencia;
    }

    public void setComiteEtica(ComiteDocencia comiteDocencia) {
        this.comiteDocencia = comiteDocencia;
    }

    public String getEditableCommitteeId() {
        return editableCommitteeId;
    }

    public void setEditableCommitteeId(String editableCommitteeId) {
        this.editableCommitteeId = editableCommitteeId;
    }

    public List<Countries> getCountries() {
        return countries;
    }

    public void setCountries(List<Countries> countries) {
        this.countries = countries;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public void setRegions(List<Regions> regions) {
        this.regions = regions;
    }

    public List<Cities> getCities() {
        return cities;
    }

    public void setCities(List<Cities> cities) {
        this.cities = cities;
    }

    public Countries getCountrySelected() {
        return countrySelected;
    }

    public void setCountrySelected(Countries countrySelected) {
        this.countrySelected = countrySelected;
    }

    public Regions getRegionSelected() {
        return regionSelected;
    }

    public void setRegionSelected(Regions regionSelected) {
        this.regionSelected = regionSelected;
    }

    public Cities getCitySelected() {
        return citySelected;
    }

    public void setCitySelected(Cities citySelected) {
        this.citySelected = citySelected;
    }

    public ContactPerson getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(ContactPerson contactPerson) {
        this.contactPerson = contactPerson;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Collection<ContactPerson> getContactList() {
        return comiteDocencia.getContactPersons();
    }

    public void setContactList(Collection<ContactPerson> contactList) {
        comiteDocencia.setContactPersons(contactList);
    }

    public String getEditContactPersonId() {
        return editContactPersonId;
    }

    public void setEditContactPersonId(String editContactPersonId) {
        this.editContactPersonId = editContactPersonId;
    }

    public List<Centro> getCenters() {
        return this.centroService.list();
    }

    public void setCenters(List<Centro> centers) {
        this.centers = centers;
    }
}
